package io.qifan.desktop.server.domain.user;

import io.qifan.desktop.common.jpa.BaseEntity;
import io.qifan.desktop.server.domain.device.Device;
import lombok.*;
import org.hibernate.Hibernate;
import org.hibernate.validator.constraints.Length;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Objects;

@Entity
@Table(name = "user", indexes = @Index(unique = true, columnList = "username"))
@Getter
@Setter
@ToString
@RequiredArgsConstructor
public class User extends BaseEntity {


    @NotBlank(message = "用户名不能为空")
    @Length(min = 2, max = 20, message = "用户名长度介于2-20")
    private String username;

    @NotBlank(message = "密码不能为空")
    @Length(min = 6, max = 20, message = "密码长度介于6-20")
    private String password;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @ToString.Exclude
    private List<Device> devices;

    public void checkPassword(String requestPassword) {
        boolean equals = password.equals(requestPassword);
        if (!equals) throw new RuntimeException("密码不正确");
    }
}
